package dao;

import java.util.ArrayList;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import beans.Agent;
import beans.Branch;
import beans.Terminal;

import utils.HibernateUtil;




public class TerminalDao {

	private SessionFactory sessionFactory;
	private Transaction tr = null;

	public TerminalDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}
	
	BranchDao branchDao = new BranchDao();
	
	/**
	 * 删除终端
	 */
	public void delete(Terminal terminal) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(terminal);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改终端
	 */
	public void edit(Terminal terminal) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(terminal);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有终端(agent)
	 * @return
	 */
	public ArrayList<Terminal> returnAllTermials(Agent agent) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		ArrayList<Terminal> terminals = null;
		Criteria c = session.createCriteria(Terminal.class);
		c.add(Restrictions.in("branch", branchDao.getAll(agent)));
		c.addOrder(Order.asc("terminalId"));
		terminals = (ArrayList<Terminal>) c.list();
		tr.commit();
		session.close();
		return terminals;
	}
	
	public ArrayList<Terminal> getTerminalPage(Agent agent,int rows,int page) {
	    Session session = sessionFactory.openSession();
	    tr = session.beginTransaction();
	    ArrayList<Terminal> terminals = null;
	    Criteria c = session.createCriteria(Terminal.class);
	    c.add(Restrictions.in("branch", branchDao.getAll(agent)));
	    c.setFirstResult((page-1)*rows);
        c.setMaxResults(rows);
	    c.addOrder(Order.asc("terminalId"));
	    terminals = (ArrayList<Terminal>) c.list();
	    tr.commit();
	    session.close();
	    return terminals;
	}
	
	/**
	 * 返回所有终端
	 * @return
	 */
	public ArrayList<Terminal> returnAllTermials() {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		
			ArrayList<Terminal> terminals = null;
	
			Criteria c = session.createCriteria(Terminal.class);
			c.addOrder(Order.asc("terminalId"));
			terminals = (ArrayList<Terminal>) c.list();
	
		tr.commit();
		
		
		session.close();
		return terminals;
	}
	/**
	 * 查询网点内的所有终端
	 * @param branchID 网点号
	 * @return
	 */
	public ArrayList<Terminal> returnTermialByBranchID(int branchId) {

		Branch branch=branchDao.getBranchById(branchId);
		Session session = sessionFactory.openSession();
			tr = session.beginTransaction();
		
			ArrayList<Terminal> terminals = null;
	
			Criteria c = session.createCriteria(Terminal.class);
			c.add(Restrictions.eq("branch",branch));
			terminals = (ArrayList<Terminal>) c.list();
	
			tr.commit();
		session.close();
		return terminals;
	}
	
	/**
	 * 通过终端号返回指定终端
	 * @param terminalId
	 * @return
	 */
	public Terminal getTerminalById(int terminalId){
		
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Terminal terminal=(Terminal) session.get(Terminal.class, terminalId);
		tr.commit();
		session.close();
		return terminal;
		
	}
	/**
	 *新增终端 
	 */
	public void save(Terminal terminal){
		Session session=sessionFactory.openSession();
		
		tr=session.beginTransaction();
		session.save(terminal);
		tr.commit();
		session.close();
		
	}
	
}
